package com.sharainy.contoller;

import com.alibaba.fastjson.JSONObject;
import com.sharainy.pojo.Resource;
import com.sharainy.pojo.Role;
import com.sharainy.pojo.User;
import com.sharainy.service.IResourceService;
import com.sharainy.service.IRoleService;
import com.sharainy.service.IUserService;
import com.sharainy.util.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/system")
public class SystemController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IResourceService resourceService;
    @RequestMapping(value = "/userPage")
    public String userPage(Model model){
        List<Role> roleList = roleService.list();
        model.addAttribute("roleList",roleList);
        return "user";
    }
    @RequestMapping(value = "/rolePage")
    public String rolePage(){
        return "role";
    }
    @RequestMapping(value = "/resourcePage")
    public String resourcePage(){
        return "resource";
    }
    @RequestMapping("/userList")
    @ResponseBody
    public RestResponse userList( Integer page, Integer limit){
        if (page==null){page=1;}
        if (limit==null){limit=10;}
        JSONObject jsonMain = new JSONObject();
        List<User> searchList = userService.list();
        List<User> userList = searchList.subList((page-1)*limit, (page-1)*limit+limit>searchList.size()?searchList.size():(page-1)*limit+limit);
        jsonMain.put("userList",userList);
        jsonMain.put("count",searchList.size());
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/roleList")
    @ResponseBody
    public RestResponse roleList( Integer page, Integer limit){
        if (page==null){page=1;}
        if (limit==null){limit=10;}
        JSONObject jsonMain = new JSONObject();
        List<Role> searchList = roleService.list();
        List<Role> roleList = searchList.subList((page-1)*limit, (page-1)*limit+limit>searchList.size()?searchList.size():(page-1)*limit+limit);
        jsonMain.put("roleList",roleList);
        jsonMain.put("count",searchList.size());
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/resourceList")
    @ResponseBody
    public RestResponse resourceList( Integer page, Integer limit){
        if (page==null){page=1;}
        if (limit==null){limit=10;}
        JSONObject jsonMain = new JSONObject();
        List<Resource> searchList = resourceService.listResource();
        List<Resource> resourceList = searchList.subList((page-1)*limit, (page-1)*limit+limit>searchList.size()?searchList.size():(page-1)*limit+limit);
        jsonMain.put("resourceList",resourceList);
        jsonMain.put("count",searchList.size());
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setData(jsonMain);
        return restResponse;
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public RestResponse updateStatus(User user){
        if (user.getStatus()==0){
            user.setStatus(1);
        }else if(user.getStatus()==1){
            user.setStatus(0);
        }
        userService.update(user);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/updateUser")
    @ResponseBody
    public RestResponse updateUser(User user){
        user.setNickname(roleService.load(user.getRoleid()).getName());
        userService.update(user);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/updateRole")
    @ResponseBody
    public RestResponse updateRole(Role role){
        roleService.update(role);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/updateResource")
    @ResponseBody
    public RestResponse updateResource(Resource resource){
        resourceService.update(resource);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/delUser")
    @ResponseBody
    public RestResponse delUser(Integer id){
        roleService.deleteUserRoles(id);
        userService.delete(id);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/addUser")
    @ResponseBody
    public RestResponse addUser(User user){
        user.setNickname(roleService.load(user.getRoleid()).getName());
        user.setStatus(0);
        user.setPassword("hzdbb123");
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        user.setCreatetime(dateNowStr);
        user.setCompanyid(13);
        List<Integer> roldIds = new ArrayList<Integer>();
        roldIds.add(user.getRoleid());
        userService.add(user,roldIds);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/addRole")
    @ResponseBody
    public RestResponse addRole(Role role){
        roleService.add(role);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/addResource")
    @ResponseBody
    public RestResponse addResource(Resource resource){
        resourceService.add(resource);
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
    @RequestMapping("/getUserResource")
    @ResponseBody
    public RestResponse getUserResource(Integer id){
        List<Resource> resourceList = userService.listAllResource(id);
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setCode(0);
        restResponse.setData(resourceList);
        return restResponse;
    }
    @RequestMapping("/getRoleResource")
    @ResponseBody
    public RestResponse getRoleResource(Integer id){
        List<Resource> resources = roleService.listRoleResource(id);
        String [] resourceStr = new String[resources.size()];
        for (int i=0;i<resources.size();i++) {
            resourceStr[i]=resources.get(i).getId()+"";
        }
        List<Resource> allResourceList = resourceService.listResource();
        for (Resource resource:allResourceList) {
            boolean res = Arrays.asList(resourceStr).contains(resource.getId()+"");
            if (res){
                resource.setStatus(1);
            }else{
                resource.setStatus(0);
            }

        }
        RestResponse restResponse = RestResponse.succuess();
        restResponse.setCode(0);
        restResponse.setData(allResourceList);
        return restResponse;
    }

    @RequestMapping("/updateRoleResource")
    @ResponseBody
    public RestResponse updateRoleResource(Integer id,Integer status,Integer roleid){
      if (status==0){
          roleService.addRoleResource(roleid,id);
      }else if (status==1){
          roleService.deleteRoleResource(roleid,id);
      }
        RestResponse restResponse = RestResponse.succuess();
        return restResponse;
    }
}
